package cn.hp.controller;


import cn.hp.entity.Announcement;
import cn.hp.service.AnnouncementService;
import cn.hp.util.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
@Api(value = "/announcement",tags = "公告管理")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;


    @GetMapping
    @ApiOperation(value = "查询",notes = "查询所有公告")
    public  Result find(Announcement announcement){
        List<Announcement> list=announcementService.findAnnouncement(announcement);
        return Result.success(list);
    }



    @GetMapping("page")
    @ApiOperation(value = "分页查询",notes = "分页查询所有公告")
    public Result page(Announcement announcement, @RequestParam(value = "page",defaultValue ="1" ) int page,@RequestParam(value = "limit",defaultValue = "10") int limit){
       PageInfo<Announcement>pageinfo=announcementService.findAnnouncement(announcement, page, limit);
       return Result.success(pageinfo);
    }

    @PostMapping
     @ApiOperation(value = "新增",notes = "新增公告")
    public  Result add(@RequestBody Announcement announcement){
      int i=announcementService.insertAnnouncement(announcement);
      return i>0 ? Result.success("添加成功"):Result.error("添加失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除",notes = "删除公告")
    public  Result delete(@PathVariable @ApiParam("删除公告") Integer id){
      int i=announcementService.deleteAnnouncementById(id);
      return i>0 ? Result.success("删除成功"):Result.error("删除失败");
    }
    @PutMapping
    @ApiOperation(value="修改",notes = "修改公告")
    public  Result update(@RequestBody @ApiParam(required = true,value = "修改公告") Announcement announcement){
      int i=announcementService.updateAnnouncement(announcement);
      return i>0 ? Result.success("修改成功"):Result.error("修改失败");
    }

}
